/// 文章模型
/// 用于表示文章详情页面的数据
class ArticleModel {
  final String id;
  final String postId;
  final String title;
  final String contentHtml;
  final int wordCount;
  final DateTime createdAt;
  final DateTime updatedAt;

  ArticleModel({
    required this.id,
    required this.postId,
    required this.title,
    required this.contentHtml,
    required this.wordCount,
    required this.createdAt,
    required this.updatedAt,
  });

  factory ArticleModel.fromJson(Map<String, dynamic> json) {
    return ArticleModel(
      id: json['id']?.toString() ?? '',
      postId: json['post_id']?.toString() ?? '',
      title: json['title'] ?? '',
      contentHtml: json['content_html'] ?? '',
      wordCount: json['word_count'] ?? 0,
      createdAt: json['created_at'] != null 
          ? DateTime.parse(json['created_at']) 
          : DateTime.now(),
      updatedAt: json['updated_at'] != null 
          ? DateTime.parse(json['updated_at']) 
          : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'post_id': postId,
      'title': title,
      'content_html': contentHtml,
      'word_count': wordCount,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  ArticleModel copyWith({
    String? id,
    String? postId,
    String? title,
    String? contentHtml,
    int? wordCount,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return ArticleModel(
      id: id ?? this.id,
      postId: postId ?? this.postId,
      title: title ?? this.title,
      contentHtml: contentHtml ?? this.contentHtml,
      wordCount: wordCount ?? this.wordCount,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
}
